﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using System.Data.SqlClient;
using System.Configuration;
using Xyz.Web.Admin.DataSet;
using Xyz.Service;
using Xyz.Service.DataTransferObjects;

namespace Xyz.Web.Admin.Report
{
    public partial class PaymentDueReport : System.Web.UI.Page
    {
        CustomerService cService = new CustomerService();
        BookingService bService = new BookingService();

        ReportDocument rDoc = null;
        PaymentDue ds = null;
        string conString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txt_DueDate.Text = DateTime.Now.ToString("dd-MMM-yyyy");
                txt_FirstName.Text = "";
                txt_LastName.Text = "";
            }
        }

        protected void btn_Preview_Click(object sender, EventArgs e)
        {
            BindReport();   
        }

        private void BindReport()
        {
            try
            {
                DateTime dtmDueDate = System.Convert.ToDateTime(txt_DueDate.Text);

                DataTable dt = new DataTable("PaymentDueData");
                DataColumn dc;
                dc = new DataColumn("R", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("CustomerId", typeof(Int32));
                dt.Columns.Add(dc);
                dc = new DataColumn("FirstName", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("LastName", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("BookingCode", typeof(string));
                dt.Columns.Add(dc);
                dc = new DataColumn("BookingDate", typeof(DateTime));
                dt.Columns.Add(dc);
                dc = new DataColumn("TotalTicketFare", typeof(decimal));
                dt.Columns.Add(dc);

                List<BookingDTO> bookings = bService.GetPaymentDueBookings(dtmDueDate, txt_FirstName.Text, txt_LastName.Text);
                List<CustomerDTO> customers = cService.GetCustomersLike(txt_FirstName.Text, txt_LastName.Text);

                foreach (BookingDTO b in bookings)
                {
                    foreach (CustomerDTO c in customers)
                    {
                        if (b.CustomerId == c.CustomerId)
                        {
                            DataRow dr = dt.NewRow();
                            dr["R"] = cService.GetCustomerTypeDescription(c.RoleId);
                            dr["CustomerId"] = c.CustomerId;
                            dr["FirstName"] = c.FirstName;
                            dr["LastName"] = c.LastName;
                            dr["BookingCode"] = b.BookingCode;
                            dr["BookingDate"] = b.BookingDate;
                            dr["TotalTicketFare"] = b.TotalTicketFare;
                            dt.Rows.Add(dr);
                            break;
                        }
                    }
                }

                rDoc = new ReportDocument();
                ds = new PaymentDue();
                ds.Tables[0].Merge(dt);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    rDoc.Load(Server.MapPath("PaymentDueRpt.rpt"));
                    rDoc.Refresh();
                    rDoc.SetDataSource(ds);
                    rDoc.SetParameterValue("dueDate", txt_DueDate.Text);
                    rDoc.SetParameterValue("firstName", txt_FirstName.Text);
                    rDoc.SetParameterValue("lastName", txt_LastName.Text);

                    crv_PaymentDue.ReportSource = rDoc;
                    crv_PaymentDue.DataBind();
                    crv_PaymentDue.Visible = true;
                    lblNoRecord.Visible = false;
                }
                else
                {
                    lblNoRecord.Visible = true;
                    crv_PaymentDue.Visible = false;
                }
            }
            catch (FormatException)
            {
                litMessage.Text = "<div class='error'> Invalid Date Format. </div>";
            }
            catch (Exception ex)
            {
                litMessage.Text = "<div class='error'> " + ex.Message + " </div>";
            }
        }

        protected void rbl_Date_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblNoRecord.Visible = false;
            crv_PaymentDue.Visible = false;
        }
    }
}